<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends CommonController 
{
	//前台首页
    public function index()
    {

    	//登陆信息显示
        $this->_userinfo($_SESSION['id']);

        //全部问题显示
        $sql = 'select t1.*,t2.username,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id order by is_top desc , is_jing desc , t1.create_time desc  limit 20';
        $quanbu = M()->query($sql);
        $this->assign('quanbu',$quanbu);


        //最近一个月回答榜 top12
        $time = strtotime('-30 days');
        $sql = 'select count(uid) as num ,t2.id ,t2.username,t2.face from answer t1 inner join user t2 on t1.uid=t2.id where t1.create_time >'.$time.' group by uid order by uid desc';
        $yueTop = M()->query($sql);
        $this->assign('yueTop',$yueTop);

        //最近热帖 10篇
        $sql = 'select * from question order by view_num desc limit 15';
        $syzuijinhot = M()->query($sql);
        $this->assign('syzuijinhot',$syzuijinhot);

        //最近热议 10篇
        $sql = 'select * from question order by answer_num desc limit 15';
        $syzuijinhotyi = M()->query($sql);
        $this->assign('syzuijinhotyi',$syzuijinhotyi);



        $this->display();
    }

    //跳入分页
    public function wenda()
    {

		$question = M('question'); 					// 实例化question对象
		//$count      = $question->where('status=1')->count();// 查询满足要求的总记录数
		$count = 	$question->count();
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		//$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
        //问题显示
        if (I('get.status')==0) {
            $sql = 'select t1.*,t2.username,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.status= "未结帖" order by is_top desc , is_jing desc , t1.create_time desc  limit '.$Page->firstRow.','.$Page->listRows;
        }elseif (I('get.status')==1) {
            $sql = 'select t1.*,t2.username,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.status= "已采纳" order by is_top desc , is_jing desc , t1.create_time desc  limit '.$Page->firstRow.','.$Page->listRows;
        }elseif (I('get.status')==2) {
            $sql = 'select t1.*,t2.username,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id where t1.is_jing= 1 order by is_top desc , is_jing desc , t1.create_time desc  limit '.$Page->firstRow.','.$Page->listRows;
        }else{
            $sql = 'select t1.*,t2.username,t2.face,t3.cname from question t1 inner join user t2 on t1.uid=t2.id inner join category t3 on t1.cid=t3.id order by is_top desc , is_jing desc , t1.create_time desc  limit '.$Page->firstRow.','.$Page->listRows;
        }
        //echo $sql;
        $list = M()->query($sql);
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出

        $this->assign('title','问答');
		$this->display(); // 输出模板


    }



}